package cn.com.mall.product.service.impl;

import cn.com.mall.product.entity.AttrEntity;
import cn.com.mall.product.vo.AttrVO;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.com.mall.common.utils.PageUtils;
import cn.com.mall.common.utils.Query;

import cn.com.mall.product.dao.AttrAttrgroupRelationDao;
import cn.com.mall.product.entity.AttrAttrgroupRelationEntity;
import cn.com.mall.product.service.AttrAttrgroupRelationService;
import org.springframework.transaction.annotation.Transactional;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Autowired
    AttrAttrgroupRelationDao attrgroupRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void attrRelation(List<AttrAttrgroupRelationEntity> relationList) {

        this.saveBatch(relationList);


    }

    /**
     * 批量删除属性和属性分组的对应关系
     * @param attrRelationList
     */
    @Override
    public void deleteRelation(List<AttrVO> attrRelationList) {

//        DELETE FROM pms_attr_attrgroup_relation where  (attr_id=? and attr_group_id=? ) or (attr_id=? and attr_group_id=? )


        List<AttrAttrgroupRelationEntity> relationEntities = attrRelationList.stream().map(
                item -> {
                    AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
                    BeanUtils.copyProperties(item, relationEntity);
                    return relationEntity;
                }
        ).collect(Collectors.toList());


        attrgroupRelationDao.deleteRelation(relationEntities);


    }


}